﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Soundelle;
using Soundelle.Oscillator;

namespace Soundelle.Oscillator.GUI
{
    public partial class ControlDevice : UserControl
    {
        private Oscillator.BaseDevice OscillatorDevice = null;

        public ControlDevice()
        {
            InitializeComponent();

            InitList();

            groupParameters.Dock = DockStyle.Fill;
            textObject.Enabled = false;
            textGUID.Enabled = false;

            listFrequencyUnit.SelectedIndex = 0;
            listAmplitudeUnit.SelectedIndex = 0;
            listPhaseUnit.SelectedIndex = 0;
        }

        SortedList<Oscillator.BaseDevice.DeviceFunctionType, string> OscillatorFunctions = new SortedList<Oscillator.BaseDevice.DeviceFunctionType, string>();
        SortedList<Oscillator.BaseDevice.DeviceRoleType, string> OscillatorRoles = new SortedList<Oscillator.BaseDevice.DeviceRoleType, string>();
        private void InitList()
        {
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_Thru, "Thru Signal");
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_Generator, "Generator");
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_GeneratorSine, "Generator Sine");
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_GeneratorSquare, "Generator Square");
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_GeneratorTriangle, "Generator Triangle");
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_GeneratorRandom, "Generator Random");
            //
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_Filter, "Filter");
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_FilterLPF, "Filter LPF");
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_FilterHPF, "Filter HPF");
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_FilterAverage, "Filter Average Mean");
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_FilterAverageRMS, "Filter Average RMS");
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_FilterAverageMS, "Filter Average MS");
            //
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_Calculator, "Calculator");
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_CalculatorIntegration, "Calculator Integration");
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_CalculatorDerivation, "Calculator Derivation");
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_CalculatorTachometer, "Calculator Tachometer");
            //
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_Player, "Sound Player");
            OscillatorFunctions.Add(Oscillator.BaseDevice.DeviceFunctionType.Function_FileWriter, "File Writer");

            OscillatorRoles.Add(Oscillator.BaseDevice.DeviceRoleType.Role_Mixer_MixingSignal, "Mixer Mixing Signal");
            OscillatorRoles.Add(Oscillator.BaseDevice.DeviceRoleType.Role_Mixer_InputSignal, "Mixer Input Signal");
            OscillatorRoles.Add(Oscillator.BaseDevice.DeviceRoleType.Role_Mixer_DisableSignal, "Mixer Disable Signal");
            OscillatorRoles.Add(Oscillator.BaseDevice.DeviceRoleType.Role_Amplitude, "Amplitude");
            OscillatorRoles.Add(Oscillator.BaseDevice.DeviceRoleType.Role_Frequency, "Frequency");
            OscillatorRoles.Add(Oscillator.BaseDevice.DeviceRoleType.Role_Phase, "Phase");
            OscillatorRoles.Add(Oscillator.BaseDevice.DeviceRoleType.Role_Duty, "Duty");
            OscillatorRoles.Add(Oscillator.BaseDevice.DeviceRoleType.Role_Modulator_Amplitude, "Modulator Amplitude");
            OscillatorRoles.Add(Oscillator.BaseDevice.DeviceRoleType.Role_Modulator_Frequency, "Modulator Frequency");
            OscillatorRoles.Add(Oscillator.BaseDevice.DeviceRoleType.Role_Modulator_Phase, "Modulator Phase");
            OscillatorRoles.Add(Oscillator.BaseDevice.DeviceRoleType.Role_Modulator_Duty, "Modulator Duty");
            OscillatorRoles.Add(Oscillator.BaseDevice.DeviceRoleType.Role_Multiplier_Amplitude, "Multiplier Amplitude");
            OscillatorRoles.Add(Oscillator.BaseDevice.DeviceRoleType.Role_Multiplier_Frequency, "Multiplier Frequency");
            OscillatorRoles.Add(Oscillator.BaseDevice.DeviceRoleType.Role_Multiplier_Phase, "Multiplier Phase");
            OscillatorRoles.Add(Oscillator.BaseDevice.DeviceRoleType.Role_Multiplier_Duty, "Multiplier Duty");

            listRole.Items.Clear();
            for (int i = 0; i < OscillatorRoles.Count; ++i)
            {
                listRole.Items.Add(OscillatorRoles.Values[i]);
            }

            UpdateComboBoxAsDuty(ref comboDuty);
        }

        public void AttachDevice(ref Oscillator.BaseDevice Device)
        {
            DettachDevice();
            trackAmplitudeDb.Value = 0;

            OscillatorDevice = Device;
            UpdateView();
        }

        public void DettachDevice()
        {
            OscillatorDevice = null;

            textObject.Text = "";
            textGUID.Text = "";
        }

        bool IsUpdatingView = false;
        private void UpdateView()
        {
            if (OscillatorDevice == null)
                return;

            IsUpdatingView = true;

            textObject.Text = OscillatorDevice.GetType().ToString();
            textGUID.Text = OscillatorDevice.GUID.ToString();

            listFunction.Items.Clear();
            for (int i = 0; i < OscillatorDevice.AvailableFunctions.Length; ++i)
            {
                listFunction.Items.Add(FunctionToText(OscillatorDevice.AvailableFunctions[i]));
            }

            listRole.Text = RoleToText(OscillatorDevice.Role);
            listFunction.Text = FunctionToText(OscillatorDevice.Function);

            checkEnabled.Checked = OscillatorDevice.Enabled;

            panelDcOffset.Visible = false;
            panelAmplitude.Visible = false;
            panelAmplitudeDb.Visible = false;
            panelFrequency.Visible = false;
            panelPhase.Visible = false;
            panelDuty.Visible = false;
            panelIsZeroPeak.Visible = false;
            panelDistribution.Visible = false;
            panelAverages.Visible = false;
            panelCount.Visible = false;

            foreach (Oscillator.BaseDevice.ParameterType param in OscillatorDevice.AvailableParameters)
            {
                switch (param)
                {
                    case Oscillator.BaseDevice.ParameterType.Parameter_DC_Offset:
                        comboDcOffset.Text = OscillatorDevice.Parameter_DC_Offset.ToString();
                        panelDcOffset.Visible = true;
                        break;
                    case Oscillator.BaseDevice.ParameterType.Parameter_Amplitude:
                        double dBrate = Math.Pow(10, ((double)trackAmplitudeDb.Value / 20.0));

                        if (listAmplitudeUnit.Text == "RMS")
                            comboAmplitude.Text = ( OscillatorDevice.Parameter_AmplitudeRms / dBrate).ToString();
                        else if (listAmplitudeUnit.Text == "RPM")
                            comboAmplitude.Text = (60.0 * OscillatorDevice.Parameter_Amplitude / dBrate).ToString();
                        else if (listAmplitudeUnit.Text == "Degree")
                            comboAmplitude.Text = (180.0 / Math.PI * OscillatorDevice.Parameter_Amplitude / dBrate).ToString();
                        else //if (listAmplitudeUnit.Text == "Peak")
                            comboAmplitude.Text = ( OscillatorDevice.Parameter_Amplitude / dBrate).ToString();

                        panelAmplitude.Visible = true;
                        panelAmplitudeDb.Visible = true;
                        break;
                    case Oscillator.BaseDevice.ParameterType.Parameter_Frequency:
                        if (listFrequencyUnit.Text == "RPM")
                            comboFrequency.Text = (60.0 * OscillatorDevice.Parameter_Frequency).ToString();
                        else //if (listFrequencyUnit.Text == "Hz")
                            comboFrequency.Text = OscillatorDevice.Parameter_Frequency.ToString();

                        panelFrequency.Visible = true;
                        break;
                    case Oscillator.BaseDevice.ParameterType.Parameter_Phase:
                        if (listPhaseUnit.Text == "Radian")
                            comboPhase.Text = OscillatorDevice.Parameter_Phase.ToString();
                        else if (listPhaseUnit.Text == "Degree")
                            comboPhase.Text = (180.0 / Math.PI * OscillatorDevice.Parameter_Phase).ToString();
                        panelPhase.Visible = true;
                        break;
                    case Oscillator.BaseDevice.ParameterType.Parameter_Duty:
                        comboDuty.Text = OscillatorDevice.Parameter_Duty.ToString();
                        panelDuty.Visible = true;
                        break;
                    case Oscillator.BaseDevice.ParameterType.Parameter_IsZeroPeak:
                        checkIsZeroPeak.Checked = OscillatorDevice.Parameter_IsZeroPeak;
                        panelIsZeroPeak.Visible = true;
                        break;
                    case Oscillator.BaseDevice.ParameterType.Parameter_Distribution:
                        comboDistribution.Text = OscillatorDevice.Parameter_Distribution.ToString();
                        panelDistribution.Visible = true;
                        break;
                    case Oscillator.BaseDevice.ParameterType.Parameter_Averages:
                        comboAverages.Text = OscillatorDevice.Parameter_Averages.ToString();
                        panelAverages.Visible = true;
                        break;
                    case Oscillator.BaseDevice.ParameterType.Parameter_Count:
                        panelCount.Text = OscillatorDevice.Parameter_Count.ToString();
                        panelCount.Visible = true;
                        break;
                    default:
                        break;
                }
            }

            IsUpdatingView = false;
        }

        public string FunctionToText(Oscillator.BaseDevice.DeviceFunctionType Function)
        {
            if (OscillatorFunctions.ContainsKey(Function))
            {
                return OscillatorFunctions.Values[OscillatorFunctions.IndexOfKey(Function)];
            }
            else
            {
                return "";
            }
        }


        public Oscillator.BaseDevice.DeviceFunctionType TextToFunction(string Text)
        {
            if (OscillatorFunctions.ContainsValue(Text))
            {
                return OscillatorFunctions.Keys[OscillatorFunctions.IndexOfValue(Text)];
            }
            else
            {
                return Oscillator.BaseDevice.DeviceFunctionType.Function_Thru;
            }
        }

        public string RoleToText(Oscillator.BaseDevice.DeviceRoleType Role)
        {
            if (OscillatorRoles.ContainsKey(Role))
            {
                return OscillatorRoles.Values[OscillatorRoles.IndexOfKey(Role)];
            }
            else
            {
                return "";
            }
        }

        public Oscillator.BaseDevice.DeviceRoleType TextToRole(string Text)
        {
            if (OscillatorRoles.ContainsValue(Text))
            {
                return OscillatorRoles.Keys[OscillatorRoles.IndexOfValue(Text)];
            }
            else
            {
                return Oscillator.BaseDevice.DeviceRoleType.Role_Mixer_MixingSignal;
            }
        }

        #region "Init ComboBox"


        private void UpdateComboBoxAsAmplitude(ref ComboBox combo)
        {
            combo.Items.Clear();

            combo.Items.Add("-5");
            combo.Items.Add("-4.5");
            combo.Items.Add("-4");
            combo.Items.Add("-3.5");
            combo.Items.Add("-3");
            combo.Items.Add("-2.5");
            combo.Items.Add("-2");
            combo.Items.Add("-1.5");
            combo.Items.Add("-1");
            combo.Items.Add("-0.5");
            combo.Items.Add("0");
            combo.Items.Add("0.5");
            combo.Items.Add("1");
            combo.Items.Add("1.5");
            combo.Items.Add("2");
            combo.Items.Add("2.5");
            combo.Items.Add("3");
            combo.Items.Add("3.5");
            combo.Items.Add("4");
            combo.Items.Add("4.5");
            combo.Items.Add("5");
        }

        private void UpdateComboBoxAsDuty(ref ComboBox combo)
        {
            combo.Items.Clear();

            combo.Items.Add("0");
            combo.Items.Add("0.1");
            combo.Items.Add("0.2");
            combo.Items.Add("0.3");
            combo.Items.Add("0.4");
            combo.Items.Add("0.5");
            combo.Items.Add("0.6");
            combo.Items.Add("0.7");
            combo.Items.Add("0.8");
            combo.Items.Add("0.9");
            combo.Items.Add("1");
        }

        private void UpdateComboBoxAsMusicHz(ref ComboBox combo)
        {
            combo.Items.Clear();

            combo.Items.Add("27.5");
            combo.Items.Add("29.13523509488");
            combo.Items.Add("30.86770632851");
            combo.Items.Add("32.70319566257");
            combo.Items.Add("34.64782887211");
            combo.Items.Add("36.70809598968");
            combo.Items.Add("38.89087296526");
            combo.Items.Add("41.20344461411");
            combo.Items.Add("43.65352892913");
            combo.Items.Add("46.24930283895");
            combo.Items.Add("48.99942949772");
            combo.Items.Add("51.91308719749");
            combo.Items.Add("55");
            combo.Items.Add("58.27047018976");
            combo.Items.Add("61.73541265702");
            combo.Items.Add("65.40639132515");
            combo.Items.Add("69.29565774422");
            combo.Items.Add("73.41619197935");
            combo.Items.Add("77.78174593052");
            combo.Items.Add("82.40688922822");
            combo.Items.Add("87.30705785825");
            combo.Items.Add("92.49860567791");
            combo.Items.Add("97.99885899544");
            combo.Items.Add("103.82617439499");
            combo.Items.Add("110");
            combo.Items.Add("116.54094037952");
            combo.Items.Add("123.47082531403");
            combo.Items.Add("130.81278265030");
            combo.Items.Add("138.59131548844");
            combo.Items.Add("146.83238395870");
            combo.Items.Add("155.56349186104");
            combo.Items.Add("164.81377845644");
            combo.Items.Add("174.61411571650");
            combo.Items.Add("184.99721135582");
            combo.Items.Add("195.99771799088");
            combo.Items.Add("207.65234878997");
            combo.Items.Add("220");
            combo.Items.Add("233.08188075905");
            combo.Items.Add("246.94165062806");
            combo.Items.Add("261.62556530060");
            combo.Items.Add("277.18263097687");
            combo.Items.Add("293.66476791741");
            combo.Items.Add("311.12698372208");
            combo.Items.Add("329.62755691287");
            combo.Items.Add("349.22823143300");
            combo.Items.Add("369.99442271163");
            combo.Items.Add("391.99543598175");
            combo.Items.Add("415.30469757995");
            combo.Items.Add("440");
            combo.Items.Add("466.16376151809");
            combo.Items.Add("493.88330125612");
            combo.Items.Add("523.25113060120");
            combo.Items.Add("554.36526195374");
            combo.Items.Add("587.32953583482");
            combo.Items.Add("622.25396744416");
            combo.Items.Add("659.25511382574");
            combo.Items.Add("698.45646286601");
            combo.Items.Add("739.98884542327");
            combo.Items.Add("783.99087196350");
            combo.Items.Add("830.60939515989");
            combo.Items.Add("880");
            combo.Items.Add("932.32752303618");
            combo.Items.Add("987.76660251225");
            combo.Items.Add("1046.50226120239");
            combo.Items.Add("1108.73052390749");
            combo.Items.Add("1174.65907166963");
            combo.Items.Add("1244.50793488832");
            combo.Items.Add("1318.51022765148");
            combo.Items.Add("1396.91292573202");
            combo.Items.Add("1479.97769084654");
            combo.Items.Add("1567.98174392700");
            combo.Items.Add("1661.21879031978");
            combo.Items.Add("1760");
            combo.Items.Add("1864.65504607236");
            combo.Items.Add("1975.53320502450");
            combo.Items.Add("2093.00452240479");
            combo.Items.Add("2217.46104781498");
            combo.Items.Add("2349.31814333926");
            combo.Items.Add("2489.01586977665");
            combo.Items.Add("2637.02045530296");
            combo.Items.Add("2793.82585146403");
            combo.Items.Add("2959.95538169308");
            combo.Items.Add("3135.96348785399");
            combo.Items.Add("3322.43758063956");
            combo.Items.Add("3520");
            combo.Items.Add("3729.31009214472");
            combo.Items.Add("3951.06641004899");
            combo.Items.Add("4186.00904480958");
            combo.Items.Add("4434.92209562995");
            combo.Items.Add("4698.63628667852");
            combo.Items.Add("4978.03173955329");
            combo.Items.Add("5274.04091060592");
            combo.Items.Add("5587.65170292806");
            combo.Items.Add("5919.91076338615");
            combo.Items.Add("6271.92697570799");
            combo.Items.Add("6644.87516127912");
            combo.Items.Add("7040");
        }

        private void UpdateComboBoxAsFreqHz(ref ComboBox combo)
        {
            combo.Items.Clear();

            combo.Items.Add("0.1");
            combo.Items.Add("0.125");
            combo.Items.Add("0.16");
            combo.Items.Add("0.2");
            combo.Items.Add("0.25");
            combo.Items.Add("0.315");
            combo.Items.Add("0.4");
            combo.Items.Add("0.5");
            combo.Items.Add("0.63");
            combo.Items.Add("0.8");
            combo.Items.Add("1");
            combo.Items.Add("1.25");
            combo.Items.Add("1.6");
            combo.Items.Add("2");
            combo.Items.Add("2.5");
            combo.Items.Add("3.15");
            combo.Items.Add("4");
            combo.Items.Add("5");
            combo.Items.Add("6.3");
            combo.Items.Add("8");
            combo.Items.Add("10");
            combo.Items.Add("12.5");
            combo.Items.Add("16");
            combo.Items.Add("20");
            combo.Items.Add("25");
            combo.Items.Add("31.5");
            combo.Items.Add("40");
            combo.Items.Add("50");
            combo.Items.Add("63");
            combo.Items.Add("80");
            combo.Items.Add("100");
            combo.Items.Add("125");
            combo.Items.Add("160");
            combo.Items.Add("200");
            combo.Items.Add("250");
            combo.Items.Add("315");
            combo.Items.Add("400");
            combo.Items.Add("500");
            combo.Items.Add("630");
            combo.Items.Add("800");
            combo.Items.Add("1000");
            combo.Items.Add("1250");
            combo.Items.Add("1600");
            combo.Items.Add("2000");
            combo.Items.Add("2500");
            combo.Items.Add("3150");
            combo.Items.Add("4000");
            combo.Items.Add("5000");
            combo.Items.Add("6300");
            combo.Items.Add("8000");
            combo.Items.Add("10000");
            combo.Items.Add("12500");
            combo.Items.Add("16000");
            combo.Items.Add("20000");
            combo.Items.Add("25000");
            combo.Items.Add("31500");
            combo.Items.Add("40000");
        }

        private void UpdateComboBoxAsFreqRpm(ref ComboBox combo)
        {
            combo.Items.Clear();

            combo.Items.Add("5");
            combo.Items.Add("10");
            combo.Items.Add("12.5");
            combo.Items.Add("15");
            combo.Items.Add("20");
            combo.Items.Add("25");
            combo.Items.Add("30");
            combo.Items.Add("50");
            combo.Items.Add("60");
            combo.Items.Add("80");
            combo.Items.Add("100");
            combo.Items.Add("100");
            combo.Items.Add("125");
            combo.Items.Add("150");
            combo.Items.Add("200");
            combo.Items.Add("250");
            combo.Items.Add("300");
            combo.Items.Add("500");
            combo.Items.Add("600");
            combo.Items.Add("800");
            combo.Items.Add("1000");
            combo.Items.Add("1000");
            combo.Items.Add("1250");
            combo.Items.Add("1500");
            combo.Items.Add("2000");
            combo.Items.Add("2500");
            combo.Items.Add("3000");
            combo.Items.Add("5000");
            combo.Items.Add("6000");
            combo.Items.Add("8000");
            combo.Items.Add("10000");
            combo.Items.Add("10000");
            combo.Items.Add("12500");
            combo.Items.Add("15000");
            combo.Items.Add("20000");
            combo.Items.Add("25000");
            combo.Items.Add("30000");
        }

        private void UpdateComboBoxAsPhaseRadian(ref ComboBox combo)
        {
            combo.Items.Clear();

            combo.Items.Add("-3.14159265358979");
            combo.Items.Add("-2.87979326579064");
            combo.Items.Add("-2.61799387799149");
            combo.Items.Add("-2.35619449019234");
            combo.Items.Add("-2.09439510239320");
            combo.Items.Add("-1.83259571459405");
            combo.Items.Add("-1.57079632679490");
            combo.Items.Add("-1.30899693899575");
            combo.Items.Add("-1.04719755119660");
            combo.Items.Add("-0.78539816339745");
            combo.Items.Add("-0.52359877559830");
            combo.Items.Add("-0.26179938779915");
            combo.Items.Add("0.0");
            combo.Items.Add("0.26179938779915");
            combo.Items.Add("0.52359877559830");
            combo.Items.Add("0.78539816339745");
            combo.Items.Add("1.04719755119660");
            combo.Items.Add("1.30899693899575");
            combo.Items.Add("1.57079632679490");
            combo.Items.Add("1.83259571459405");
            combo.Items.Add("2.09439510239320");
            combo.Items.Add("2.35619449019234");
            combo.Items.Add("2.61799387799149");
            combo.Items.Add("2.87979326579064");
            combo.Items.Add("3.14159265358979");
        }

        private void UpdateComboBoxAsPhaseDegree(ref ComboBox combo)
        {
            combo.Items.Clear();

            combo.Items.Add("-180");
            combo.Items.Add("-165");
            combo.Items.Add("-150");
            combo.Items.Add("-135");
            combo.Items.Add("-120");
            combo.Items.Add("-105");
            combo.Items.Add("-90");
            combo.Items.Add("-75");
            combo.Items.Add("-60");
            combo.Items.Add("-45");
            combo.Items.Add("-30");
            combo.Items.Add("-15");
            combo.Items.Add("0");
            combo.Items.Add("15");
            combo.Items.Add("30");
            combo.Items.Add("45");
            combo.Items.Add("60");
            combo.Items.Add("75");
            combo.Items.Add("90");
            combo.Items.Add("105");
            combo.Items.Add("120");
            combo.Items.Add("135");
            combo.Items.Add("150");
            combo.Items.Add("165");
            combo.Items.Add("180");
        }

        #endregion

        
        private void comboFrequencyUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (listFrequencyUnit.Text)
            {
                case "Hz":
                    UpdateComboBoxAsFreqHz(ref comboFrequency);
                    break;

                case "RPM":
                    UpdateComboBoxAsFreqRpm(ref comboFrequency);
                    break;

                case "Hz(m)":
                    UpdateComboBoxAsMusicHz(ref comboFrequency);
                    break;

                default:
                    break;
            }
            UpdateView();

        }

        private void listAmplitudeUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (listAmplitudeUnit.Text)
            {
                case "Peak":
                case "RMS":
                    UpdateComboBoxAsAmplitude(ref comboAmplitude);
                    break;

                case "Hz":
                    UpdateComboBoxAsFreqHz(ref comboAmplitude);
                    break;

                case "RPM":
                    UpdateComboBoxAsFreqRpm(ref comboAmplitude);
                    break;

                case "Hz(m)":
                    UpdateComboBoxAsMusicHz(ref comboAmplitude);
                    break;

                case "Radian":
                    UpdateComboBoxAsPhaseRadian(ref comboAmplitude);
                    break;

                case "Degree":
                    UpdateComboBoxAsPhaseDegree(ref comboAmplitude);
                    break;

                case "Duty":
                    UpdateComboBoxAsDuty(ref comboAmplitude);
                    break;

                default:
                    break;
            }

            UpdateView();
        }

        private void listPhaseUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (listPhaseUnit.Text)
            {
                case "Radian":
                    UpdateComboBoxAsPhaseRadian(ref comboPhase);
                    break;

                case "Degree":
                    UpdateComboBoxAsPhaseDegree(ref comboPhase);
                    break;

                default:
                    break;
            }
            UpdateView();
        }

        //
        private void listFunction_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsUpdatingView == false && OscillatorDevice != null)
            {
                OscillatorDevice.Function = TextToFunction(listFunction.Text);
                UpdateView();
            }
        }

        private void listRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsUpdatingView == false && OscillatorDevice != null)
            {
                OscillatorDevice.Role = TextToRole(listRole.Text);

                switch (OscillatorDevice.Role)
                {
                    case BaseDevice.DeviceRoleType.Role_Frequency:
                    case BaseDevice.DeviceRoleType.Role_Modulator_Frequency:
                    case BaseDevice.DeviceRoleType.Role_Multiplier_Frequency:
                        if (listAmplitudeUnit.Items[0].ToString() != "Hz")
                        {
                            listAmplitudeUnit.Items.Clear();
                            listAmplitudeUnit.Items.Add("Hz");
                            listAmplitudeUnit.Items.Add("RPM");
                            listAmplitudeUnit.Items.Add("Hz(m)");
                            listAmplitudeUnit.SelectedIndex = 0;
                        }
                        break;

                    case BaseDevice.DeviceRoleType.Role_Phase:
                    case BaseDevice.DeviceRoleType.Role_Modulator_Phase:
                    case BaseDevice.DeviceRoleType.Role_Multiplier_Phase:
                        if (listAmplitudeUnit.Items[0].ToString() != "Radian")
                        {
                            listAmplitudeUnit.Items.Clear();
                            listAmplitudeUnit.Items.Add("Radian");
                            listAmplitudeUnit.Items.Add("Degree");
                            listAmplitudeUnit.SelectedIndex = 0;
                        }
                        break;

                    case BaseDevice.DeviceRoleType.Role_Duty:
                    case BaseDevice.DeviceRoleType.Role_Modulator_Duty:
                    case BaseDevice.DeviceRoleType.Role_Multiplier_Duty:
                        if (listAmplitudeUnit.Items[0].ToString() != "Duty")
                        {
                            listAmplitudeUnit.Items.Clear();
                            listAmplitudeUnit.Items.Add("Duty");
                            listAmplitudeUnit.SelectedIndex = 0;
                        }
                        break;

                    default:
                        if (listAmplitudeUnit.Items[0].ToString() != "Peak")
                        {
                            listAmplitudeUnit.Items.Clear();
                            listAmplitudeUnit.Items.Add("Peak");
                            listAmplitudeUnit.Items.Add("RMS");
                            listAmplitudeUnit.SelectedIndex = 0;
                        }
                        break;
                }
            }
        }

        private void comboDcOffset_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsUpdatingView == false && OscillatorDevice != null)
            {
                OscillatorDevice.Parameter_DC_Offset = double.Parse(comboDcOffset.Text);
            }
        }

        private void comboAmplitude_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsUpdatingView == false && OscillatorDevice != null)
            {
                double dBrate = Math.Pow(10, ((double)trackAmplitudeDb.Value / 20.0));

                if (listAmplitudeUnit.Text == "RMS")
                    OscillatorDevice.Parameter_AmplitudeRms =dBrate * double.Parse(comboAmplitude.Text);
                else if (listAmplitudeUnit.Text == "RPM")
                    OscillatorDevice.Parameter_Amplitude =dBrate * double.Parse(comboAmplitude.Text) / 60.0;
                else if (listAmplitudeUnit.Text == "Degree")
                    OscillatorDevice.Parameter_Amplitude = dBrate * Math.PI / 180.0 * double.Parse(comboAmplitude.Text);
                else //if (listAmplitudeUnit.Text == "Peak")
                    OscillatorDevice.Parameter_Amplitude =dBrate * double.Parse(comboAmplitude.Text);

            }
        }

        private void comboFrequency_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsUpdatingView == false && OscillatorDevice != null)
            {
                if (listFrequencyUnit.Text == "RPM")
                    OscillatorDevice.Parameter_Frequency = double.Parse(comboFrequency.Text) / 60.0;
                else //if (listFrequencyUnit.Text == "Hz")
                    OscillatorDevice.Parameter_Frequency = double.Parse(comboFrequency.Text);
            }
        }

        private void comboPhase_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsUpdatingView == false && OscillatorDevice != null)
            {
                if (listPhaseUnit.Text == "Radian")
                    OscillatorDevice.Parameter_Phase = double.Parse(comboPhase.Text);
                else if (listPhaseUnit.Text == "Degree")
                    OscillatorDevice.Parameter_Phase = Math.PI / 180.0 * double.Parse(comboPhase.Text);
            }
        }

        private void comboDuty_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsUpdatingView == false && OscillatorDevice != null)
            {
                OscillatorDevice.Parameter_Duty = double.Parse(comboDuty.Text);
                UpdateView();
            }
        }

        private void checkIsZeroPeak_CheckedChanged(object sender, EventArgs e)
        {
            if (IsUpdatingView == false && OscillatorDevice != null)
            {
                OscillatorDevice.Parameter_IsZeroPeak = checkIsZeroPeak.Checked;
                UpdateView();
            }
        }

        private void comboDistribution_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsUpdatingView == false && OscillatorDevice != null)
            {
                OscillatorDevice.Parameter_Distribution = double.Parse(comboDistribution.Text);
            }
        }

        private void comboAverages_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsUpdatingView == false && OscillatorDevice != null)
            {
                OscillatorDevice.Parameter_Averages = long.Parse(comboAverages.Text);
            }
        }

        private void comboCount_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsUpdatingView == false && OscillatorDevice != null)
            {
                OscillatorDevice.Parameter_Count = double.Parse(comboCount.Text);
            }
        }

        private void comboDcOffset_TextChanged(object sender, EventArgs e)
        {
            if (OscillatorDevice == null)
                return;

            double value = 0.0;

            if (double.TryParse(comboDcOffset.Text, out value))
            {
                comboDcOffset.BackColor = Color.White;
                OscillatorDevice.Parameter_DC_Offset = value;
            }
            else
            {
                comboDcOffset.BackColor = Color.LightPink;
            }
        }

        private void comboAmplitude_TextChanged(object sender, EventArgs e)
        {
            if (OscillatorDevice == null)
                return;

            double value = 0.0;

            if (double.TryParse(comboAmplitude.Text, out value))
            {
                comboAmplitude.BackColor = Color.White;

                double dBrate = Math.Pow(10, ((double)trackAmplitudeDb.Value / 20.0));
                value *= dBrate;

                if (listAmplitudeUnit.Text == "RMS")
                    OscillatorDevice.Parameter_AmplitudeRms = value;
                else if (listAmplitudeUnit.Text == "RPM")
                    OscillatorDevice.Parameter_Amplitude = value / 60.0;
                else if (listAmplitudeUnit.Text == "Degree")
                    OscillatorDevice.Parameter_Amplitude = Math.PI / 180.0 * value;
                else //if (listAmplitudeUnit.Text == "Peak")
                    OscillatorDevice.Parameter_Amplitude = value;
            }
            else
            {
                comboAmplitude.BackColor = Color.LightPink;
            }
        }

        private void comboFrequency_TextChanged(object sender, EventArgs e)
        {
            if (OscillatorDevice == null)
                return;

            double value = 0.0;

            if (double.TryParse(comboFrequency.Text, out value))
            {
                comboFrequency.BackColor = Color.White;

                if (listFrequencyUnit.Text == "RPM")
                    OscillatorDevice.Parameter_Frequency = value / 60.0;
                else //if (listFrequencyUnit.Text == "Hz")
                    OscillatorDevice.Parameter_Frequency = value;
            }
            else
            {
                comboFrequency.BackColor = Color.LightPink;
            }
        }

        private void comboPhase_TextChanged(object sender, EventArgs e)
        {
            if (OscillatorDevice == null)
                return;

            double value = 0.0;

            if (double.TryParse(comboPhase.Text, out value))
            {
                comboPhase.BackColor = Color.White;

                if (listPhaseUnit.Text == "Radian")
                    OscillatorDevice.Parameter_Phase = value;
                else if (listPhaseUnit.Text == "Degree")
                    OscillatorDevice.Parameter_Phase = Math.PI / 180.0 * value;
            }
            else
            {
                comboPhase.BackColor = Color.LightPink;
            }
        }

        private void comboDuty_TextChanged(object sender, EventArgs e)
        {
            if (OscillatorDevice == null)
                return;

            double value = 0.0;

            if (double.TryParse(comboDuty.Text, out value))
            {
                comboDuty.BackColor = Color.White;

                if (0 <= value && value <= 1.0)
                    OscillatorDevice.Parameter_Duty = value;
            }
            else
            {
                comboDuty.BackColor = Color.LightPink;
            }
        }

        private void comboDuty_Leave(object sender, EventArgs e)
        {
            UpdateView();
        }

        private void comboDistribution_TextChanged(object sender, EventArgs e)
        {
            if (OscillatorDevice == null)
                return;

            double value = 0.0;

            if (double.TryParse(comboDistribution.Text, out value))
            {
                comboDistribution.BackColor = Color.White;
                OscillatorDevice.Parameter_Distribution = value;
            }
            else
            {
                comboDistribution.BackColor = Color.LightPink;
            }
        }

        private void comboAverages_TextChanged(object sender, EventArgs e)
        {
            if (OscillatorDevice == null)
                return;

            long value = 0;

            if (long.TryParse(comboAverages.Text, out value))
            {
                comboAverages.BackColor = Color.White;
                OscillatorDevice.Parameter_Averages = value;
            }
            else
            {
                comboAverages.BackColor = Color.LightPink;
            }
        }

        private void comboCount_TextChanged(object sender, EventArgs e)
        {
            if (OscillatorDevice == null)
                return;

            double value = 0.0;

            if (double.TryParse(comboCount.Text, out value))
            {
                comboCount.BackColor = Color.White;
                OscillatorDevice.Parameter_Count = value;
            }
            else
            {
                comboCount.BackColor = Color.LightPink;
            }
        }

        private void labelAmplitudeDb_Click(object sender, EventArgs e)
        {
            trackAmplitudeDb.Value = 0;
        }

        private void trackAmplitudeDb_ValueChanged(object sender, EventArgs e)
        {
            labelAmplitudeDb.Text = trackAmplitudeDb.Value.ToString() + " dB";

            if (OscillatorDevice == null)
                return;

            double value = 0.0;

            if (double.TryParse(comboAmplitude.Text, out value))
            {
                double dBrate = Math.Pow(10, ((double)trackAmplitudeDb.Value / 20.0));
                value *= dBrate;

                if (listAmplitudeUnit.Text == "RMS")
                    OscillatorDevice.Parameter_AmplitudeRms = value;
                else if (listAmplitudeUnit.Text == "RPM")
                    OscillatorDevice.Parameter_Amplitude = value / 60.0;
                else if (listAmplitudeUnit.Text == "Degree")
                    OscillatorDevice.Parameter_Amplitude = Math.PI / 180.0 * value;
                else //if (listAmplitudeUnit.Text == "Peak")
                    OscillatorDevice.Parameter_Amplitude = value;

                //UpdateView();
            }
        }

        private void checkEnabled_CheckedChanged(object sender, EventArgs e)
        {
            if (OscillatorDevice != null)
            {
                OscillatorDevice.Enabled = checkEnabled.Checked;
                flowLayoutPanel1.Enabled = checkEnabled.Checked;
                return;
            }
        }

    }
}
